package interview.huawei.tjt.training.huaweijishi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Demo03 {

    private static ArrayList<String> res = new ArrayList<>();
    private static ArrayList<Character> chList;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        chList = new ArrayList<Character>();

        for (char ch : str.toCharArray()) {
            switch (ch) {
                case '0':
                    System.out.println("-1");
                    return;
                case '2':
                    chList.add('5');
                    break;
                case '5':
                    chList.add('2');
                    break;
                case '6':
                    chList.add('9');
                    break;
                case '9':
                    chList.add('6');
                    break;
            }
            if (ch != ',') // 过滤逗号
                chList.add(ch);
        }

        Collections.sort(chList);
        int n = chList.size();

        dfs(new int[n], n, 0, "");
        Collections.sort(res, (o1, o2) -> Integer.parseInt(o1) - Integer.parseInt(o2));
        System.out.println(res);

        System.out.println(res.get(chList.get(chList.size() - 1) - '0' - 1));
    }

    private static void dfs(int[] visited, int n, int step, String temp) {
        if (temp != "")
            res.add(temp);
        // 已经是序列的最大长度
        if (step == n)
            return;
        for (int i = 0; i < n; i++) {
            if (visited[i] != 1) {
                visited[i] = 1;
                dfs(visited, n, step + 1, temp + chList.get(i));
                // 回溯
                visited[i] = 0;
            }
        }
    }

}
